package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.ProCategory;
import com.wolves.career.service.ProCategoryService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 门类（专业大类） 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-31
 */
@RestController
@RequestMapping("/career/category")
@CrossOrigin
public class ProCategoryController {
    @Autowired
    private ProCategoryService categoryService;

    @DeleteMapping("/{id}")
    public R removeCategory(@PathVariable String id) {
        categoryService.removeById(id);
        return R.ok();
    }

    @PutMapping("/category")
    public R addCategory(@RequestBody ProCategory category) {
        categoryService.save(category);
        return R.ok();
    }

    @PostMapping("/category")
    public R updateCategory(@RequestBody ProCategory category) {
        categoryService.updateById(category);
        return R.ok();
    }

    @PostMapping("/list/{pageNo}/{limit}/{categoryName}")
    public R listCategory(@PathVariable long pageNo,
                          @PathVariable long limit,
                          @PathVariable String categoryName) {
        QueryWrapper<ProCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("category_name", categoryName);
        Page<ProCategory> page = new Page<>(pageNo, limit);

        IPage<ProCategory> categoryIPage = categoryService.page(page, queryWrapper);

        return R.ok().data("records", categoryIPage.getRecords()).data("total", categoryIPage.getTotal());

    }

    /**
     * 根据学校id查询门类
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/getCategoryBySchool/{SchoolId}")
    public R getCategoryBySchool(@PathVariable String schoolId) {
        List<ProCategory> list = categoryService.getCategoryBySchoolId(schoolId);
        return R.ok().data("list",list);

    }
}

